package com.cskaoyan.service.wx;

import com.cskaoyan.bean.MarketFootprint;
import com.cskaoyan.bean.MarketFootprintExample;
import com.cskaoyan.bean.MarketGoods;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.vo.wx.footprint.WxFootPrintListInnerVo;
import com.cskaoyan.mapper.MarketFootprintMapper;
import com.cskaoyan.mapper.MarketGoodsMapper;
import com.fasterxml.jackson.databind.util.BeanUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;

/**
 * /**
 *
 * @author xyh
 * @since 2022/11/23 11:34
 */
@Service
public class WxFootPrintServiceImpl implements WxFootPrintService{
    @Autowired
    MarketGoodsMapper marketGoodsMapper;
    @Autowired
    MarketFootprintMapper marketFootprintMapper;
    @Override
    public CommonData<WxFootPrintListInnerVo> list(Integer page, Integer limit, Integer userId) {
        PageHelper.startPage(page,limit);
        // 首先查footprint表
        MarketFootprintExample marketFootprintExample = new MarketFootprintExample();
        // 按照时间排序
        marketFootprintExample.setOrderByClause("update_time desc");
        marketFootprintExample.createCriteria().andUserIdEqualTo(userId).andDeletedEqualTo(false);

        List<MarketFootprint> marketFootprints = marketFootprintMapper.selectByExample(marketFootprintExample);

        CommonData result = CommonData.data(new PageInfo(marketFootprints));
        LinkedList<WxFootPrintListInnerVo> wxFootPrintListInnerVos= new LinkedList<>();
        for (MarketFootprint marketFootprint : marketFootprints) {
            // 根据footprint去查goods表信息
            MarketGoods marketGoods = marketGoodsMapper.selectByPrimaryKey(marketFootprint.getGoodsId());
            WxFootPrintListInnerVo target = new WxFootPrintListInnerVo();
            BeanUtils.copyProperties(marketGoods,target);
            target.setId(marketFootprint.getId());
            target.setGoodsId(marketGoods.getId());
            target.setAddTime(marketFootprint.getAddTime());
            wxFootPrintListInnerVos.add(target);
        }
        result.setList(wxFootPrintListInnerVos);
        return result;
    }

    @Override
    public int delete(Integer id) {
        // 直接根据id更改deleted属性就可以了
        MarketFootprint marketFootprint = new MarketFootprint();
        marketFootprint.setId(id);
        marketFootprint.setDeleted(true);
        marketFootprintMapper.updateByPrimaryKeySelective(marketFootprint);
        return 0;
    }
}
